from django.core.files.storage import default_storage
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import pre_delete
from django.dispatch import receiver


class Users(AbstractUser):
    class Meta:
        db_table = "system_users"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime",)
    GENDER_CHOICES = (
        (0, "未知"),
        (1, "男"),
        (2, "女"),
    )
    username = models.CharField(max_length=150, unique=True, db_index=True, verbose_name="用户账号", help_text="用户账号")
    nickname = models.CharField(max_length=50, verbose_name="昵称", null=True, blank=True, help_text="昵称")
    email = models.EmailField(max_length=255, verbose_name="邮箱", null=True, blank=True, help_text="邮箱")
    mobile = models.CharField(max_length=255, verbose_name="电话", null=True, blank=True, help_text="电话")
    avatar = models.CharField(max_length=255, verbose_name="头像", null=True, blank=True, help_text="头像")
    gender = models.IntegerField(choices=GENDER_CHOICES, default=0, verbose_name="性别", null=True, blank=True, help_text="性别")
    create_datetime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间", null=True, blank=True, help_text="创建时间")
    role = models.ManyToManyField(to="Role", blank=True, verbose_name="关联角色", db_constraint=False, help_text="关联角色")


# 添加监听器，删除数据时将文件也删除
@receiver(pre_delete, sender=Users)  # sender=你要删除或修改文件字段所在的类
def avatar_delete(sender, instance, **kwargs):       # 函数名随意
    avatar = getattr(instance, 'avatar')
    if not avatar:
        return
    default_storage.delete(avatar)


class Role(models.Model):
    class Meta:
        db_table = "system_role"
        verbose_name = "角色表"
        verbose_name_plural = verbose_name
        ordering = ("sort",)
    name = models.CharField(max_length=64, verbose_name="角色名称", help_text="角色名称")
    sort = models.IntegerField(default=1, verbose_name="角色顺序", help_text="角色顺序")
    status = models.BooleanField(default=True, verbose_name="角色状态", help_text="角色状态")
    admin = models.BooleanField(default=False, verbose_name="是否为admin", help_text="是否为admin")
    remark = models.TextField(verbose_name="备注", help_text="备注", null=True, blank=True)
    permission = models.ManyToManyField(to="Permission", verbose_name="关联权限", db_constraint=False, help_text="关联权限")


class Permission(models.Model):
    class Meta:
        db_table = "system_permission"
        verbose_name = "权限表"
        verbose_name_plural = verbose_name
        ordering = ("-value",)

    METHOD_CHOICES = (
        (0, "GET"),
        (1, "POST"),
        (2, "PUT"),
        (3, "DELETE"),
    )
    name = models.CharField(max_length=64, verbose_name="名称", help_text="名称")
    value = models.CharField(max_length=64, verbose_name="权限值", help_text="权限值")
    api = models.CharField(max_length=200, verbose_name="接口地址", help_text="接口地址")
    method = models.IntegerField(default=0, verbose_name="接口请求方法", null=True, blank=True, help_text="接口请求方法")


class ApiWhiteList(models.Model):
    class Meta:
        db_table = "api_white_list"
        verbose_name = "接口白名单"
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime",)

    METHOD_CHOICES = (
        (0, "GET"),
        (1, "POST"),
        (2, "PUT"),
        (3, "DELETE"),
    )
    url = models.CharField(max_length=200, help_text="url地址", verbose_name="url")

    method = models.IntegerField(default=0, verbose_name="接口请求方法", null=True, blank=True, help_text="接口请求方法")
    enable_datasource = models.BooleanField(default=True, verbose_name="激活数据权限", help_text="激活数据权限", blank=True)
    create_datetime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间", null=True, blank=True, help_text="创建时间")


class Log(models.Model):
    class Meta:
        db_table = 'system_log'
        verbose_name = '系统日志'
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime", )

    GRADE = (
        (1, "信息"),
        (2, "警告"),
        (3, "错误"),
    )
    action = models.CharField(max_length=64, verbose_name="动作", help_text="动作")
    grade = models.IntegerField(verbose_name="等级", help_text="等级", default=1, blank=True, choices=GRADE)
    describe = models.TextField(verbose_name="详情", null=True, blank=True, help_text="详情")
    ip = models.CharField(max_length=32, verbose_name="IP", null=True, blank=True, help_text="IP")
    ip_details = models.CharField(max_length=512, verbose_name="IP详情", null=True, blank=True, help_text="IP详情")
    username = models.CharField(max_length=150, verbose_name="用户名", null=True, blank=True, help_text="用户名")
    create_datetime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间", null=True, blank=True, help_text="创建时间")






